package com.ruoyi.act.module.api;

import com.github.pagehelper.PageInfo;
import com.ruoyi.act.module.domain.ActProcess;
import com.ruoyi.act.module.service.ITaskService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 任务列表
 */
@RestController
@RequestMapping("/api/task")
public class ApiTaskController extends BaseController {

    @Autowired
    private ITaskService iTaskService;

    /**
     * 获取多个用户的任务id
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/usersTask")
    public AjaxResult findTaskList(@RequestBody ActProcess actProcess) {
        List<Map<String, Object>> list = iTaskService.findTaskList(actProcess);
        if (list == null) {
            return AjaxResult.error();
        }
        return AjaxResult.success(list);
    }

    /**
     * 撤回已提交的流程
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/withdraw")
    public AjaxResult withdraw(@RequestBody ActProcess actProcess) {
        boolean b = iTaskService.withdraw(actProcess);
        if (b) {
            List<String> ids = (List<String>) actProcess.getVariables().get("userList");
            actProcess.setTaskAssignee(ids.get(0));
            Task task = iTaskService.query(actProcess);
            if (null != task) {
                return AjaxResult.success("success", task.getId());
            }
            return AjaxResult.success("success", "");
        }
        return AjaxResult.error("撤回失败");
    }

    /**
     * 完成任务 参数
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/complete")
    public AjaxResult complete(@RequestBody ActProcess actProcess) {
        iTaskService.complete(actProcess);

        Map<String, Object> map = actProcess.getVariables();
        List<String> list = new ArrayList<>();
        if (null != map.get("userList")) {
            list.addAll((List<String>) map.get("userList"));
        } else if (null != map.get("subUserId")) {
            list.addAll((List<String>) map.get("subUserId"));
        }
        if (list.size() > 0) {
            actProcess.setTaskAssignee(list.get(0));
            Task task = iTaskService.query(actProcess);
            if (null != task) {
                return AjaxResult.success("success", task.getId());
            }
        }
        return AjaxResult.success("success", "");
    }

    /**
     * 根据taskId直接完成任务
     *
     * @param taskId
     * @return
     */
    @GetMapping("/complete/{taskId}")
    public AjaxResult complete(@PathVariable String taskId) {
        iTaskService.complete(taskId);
        return AjaxResult.success();
    }

    /**
     * 拾取任务
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/claim")
    public AjaxResult claim(@RequestBody ActProcess actProcess) {
        iTaskService.claim(actProcess);
        return AjaxResult.success();
    }

    /**
     * 归还任务 或者交接任务
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/assignee")
    public AjaxResult assignee(@RequestBody ActProcess actProcess) {
        iTaskService.setAssignee(actProcess);
        return AjaxResult.success();
    }

    /**
     * 分页查询待办任务
     *
     * @return
     */
    @PostMapping("/findPage")
    public AjaxResult findPage(@RequestBody ActProcess actProcess) {
        PageInfo pageInfo = iTaskService.findPage(actProcess);
        return AjaxResult.success(pageInfo);
    }

    /**
     * 根据 businessKey查询 代办任务
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/get")
    public AjaxResult get(@RequestBody ActProcess actProcess) {
        Task task = iTaskService.query(actProcess);
        if (task != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", task.getId());
            map.put("name", task.getName());
            map.put("category", task.getCategory());
            map.put("desc", task.getDescription());
            map.put("assignee", task.getAssignee() == null ? "" : task.getAssignee());
            map.put("businessKey", task.getBusinessKey());
            map.put("createTime", task.getCreateTime());
            return AjaxResult.success(map);
        }
        return AjaxResult.error();
    }

    /**
     * 获取代办任务列表
     *
     * @param actProcess
     * @return
     */
    @PostMapping("/list")
    public AjaxResult list(@RequestBody ActProcess actProcess) {
        List<Task> list = iTaskService.queryList(actProcess);
        List<Map<String, Object>> mapList = new ArrayList<>();

        list.forEach(info -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", info.getId());
            map.put("name", info.getName());
            map.put("category", info.getCategory());
            map.put("desc", info.getDescription());
            map.put("assignee", info.getAssignee());
            map.put("businessKey", info.getBusinessKey());
            map.put("createTime", info.getCreateTime());
            mapList.add(map);
        });
        return AjaxResult.success(mapList);
    }

}
